<?php

GenerateModuleStartPHP( App::$Current->Path . '/Modules', '' );

function GenerateModuleStartPHP( $Dir, $ModuleName )
{
	print 'Generating ' . $Dir . '/start.php (' . $ModuleName . ')<br />';
	
	// get all class files and modules
	$ClassFiles = array();
	$FunctionFiles = array();
	$Modules = array();
	$d = dir($Dir);
	while (false !== ($entry = $d->read())) 
	{
		if(strlen($entry) > 0)
		{
			// class files and module names must start with uppercase
			if(ctype_upper(substr($entry, 0, 1)) )
			{
				// if class
				if($pos = strpos($entry, '.php') )
				{
					$class = substr($entry, 0, $pos);
					$ClassFiles[$class] = $entry;
				}
				// if folder
				elseif(!strpos($entry, '.'))
				{
					if(strlen($ModuleName) > 0)
						$module = $ModuleName . '/' . $entry;
					else
						$module = $entry;
					$Modules[$module] = $entry;
					GenerateModuleStartPHP($Dir . '/' . $entry, $module);
				}
			}
			else
			{
				// if php
				if($pos = strpos($entry, '.php' ) && $entry != 'start.php' )
				{
					$FunctionFiles[] = $entry;
				}
			}
		}
	}
	
	$Ln = "\r\n";
	$Code = '<?php ' . $Ln;
	$Code .= '$module = new Module( \'' . $ModuleName . '\', \'' . $Dir . '\' );' . $Ln; 
	foreach( $ClassFiles as $class => $path )
	{
		$Code .= '$module->Classes[\'' . $class . '\'] = \'' . $path . '\';' . $Ln; 
	}
	$Code .= '$module->Activate( );' . $Ln;
	foreach( $Modules as $module => $path )
	{
		$Code .= 'require_once \'' . $path . '/start.php\';' . $Ln; 
	}
	foreach( $FunctionFiles as $path )
	{
		$Code .= 'require_once \'' . $path . '\';' . $Ln; 
	}
	
	file_put_contents($Dir . '/start.php', $Code);
}

?>